<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JSDoc: Source: utils/require.js</title>

    <script src="scripts/prettify/prettify.js"> </script>
    <script src="scripts/prettify/lang-css.js"> </script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>

<body>

<div id="main">

    <h1 class="page-title">Source: utils/require.js</h1>

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>/**
 * 按需加载js资源
 * @function require
 * @memberof module:utils
 * @param {string} resource 目标js资源地址
 * @param {function} done 资源加载完成时调用的函数
 * @author: Nixon
 * @since: 2016年10月16日
 * @example
 * import require from 'path/to/utils/require';
 * require('http://knowbox.com/js/a.js', () => {
 *     // TODO: call a method which is in a.js
 * });
 */
export default (resource, done) => {
    const node = document.createElement('script');
    node.async = true;
    node.src = resource;

    const supportOnload = 'onload' in node;
    if (supportOnload) {
        node.onload = done;
        node.onerror = function() {
            console.error(`${resource}加载失败`, node);
        };
    } else {
        node.onreadystatechange = function() {
            if (/loaded|complete/i.test(node.readyState)) {
                done.call(node);
            }
        };
    }

    document.getElementsByTagName('head')[0].appendChild(node);
};

</code></pre>
        </article>
    </section>




</div>

<nav>
    <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-components_containers.html">components/containers</a></li><li><a href="module-components_dumb.html">components/dumb</a></li><li><a href="module-constants_api.html">constants/api</a></li><li><a href="module-constants_http.html">constants/http</a></li><li><a href="module-net_httpProxy.html">net/httpProxy</a></li><li><a href="module-utils.html">utils</a></li></ul><h3>Classes</h3><ul><li><a href="module-components_containers.Welcome.html">Welcome</a></li><li><a href="module-components_dumb.MarqueeLine.html">MarqueeLine</a></li><li><a href="module-utils.QueryString.html">QueryString</a></li><li><a href="module-utils.Storage.html">Storage</a></li></ul>
</nav>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Thu Feb 09 2017 17:36:20 GMT+0800 (CST)
</footer>

<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>
